package com.xiexiaozhou.helper.admin.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xiexiaozhou.helper.core.domain.Customer;
import com.xiexiaozhou.helper.core.domain.Manager;
import com.xiexiaozhou.helper.core.domain.Order;
import com.xiexiaozhou.helper.core.service.CustomerService;
import com.xiexiaozhou.helper.core.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 订单
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private CustomerService customerService;

    /**
     * 订单列表
     *
     * @param start
     * @param limit
     * @param session
     * @param model
     * @return
     */
    @GetMapping("/index")
    public String index(
            @RequestParam(defaultValue = "0", value = "start") Integer start,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit,
            HttpSession session,
            Model model
    ) {
        Sort sort = new Sort(Sort.DEFAULT_DIRECTION, "id");
        Pageable pageable = new PageRequest(start, limit, sort);
        Page<Order> orderPage = orderService.list(pageable);
        model.addAttribute("orderPage", orderPage);
        return "order/index";
    }

    /**
     * 进入指派页面
     *
     * @param orderId
     * @param model
     * @return
     */
    @GetMapping("/assign")
    public String assign(Long orderId, Model model) {
        Order order = orderService.get(orderId);
        model.addAttribute("order", order);
        List<Customer> distributors = customerService.listAllDistributor();
        model.addAttribute("distributors", distributors);

        return "order/assign";
    }

    /**
     * 执行指派操作
     *
     * @param id
     * @param distributorId
     * @param session
     * @param model
     * @return
     */
    @PostMapping("/assign")
    public String assign(
            @RequestParam Long id,
            @RequestParam Long distributorId,
            HttpSession session,
            Model model
    ) {
        //获取指派人
        Manager manager = (Manager) session.getAttribute("manager");

        //执行指派操作
        Order order = orderService.assign(id, manager.getId(), distributorId);

        model.addAttribute("order",order);

        model.addAttribute("tip", "指派成功");

        return "order/detail";
    }

    /**
     * 指派
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/detail")
    public String detail(@RequestParam Long id, Model model) {
        Order order = orderService.get(id);
        model.addAttribute("order", order);
        return "order/detail";
    }
}
